﻿package frEngine.effectEditTool.temple
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import flash.utils.*;
    import frEngine.animateControler.*;
    import frEngine.animateControler.keyframe.*;
    import frEngine.core.mesh.*;
    import frEngine.effectEditTool.manager.*;
    import frEngine.effectEditTool.parser.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.filters.fragmentFilters.extra.*;
    import frEngine.shader.filters.vertexFilters.*;
    import frEngine.shader.md5shader.*;

    public class TempleRole extends Object implements ITemple
    {
        private var _targetMesh:Md5Mesh;
        private var _playerList:Vector.<Mesh3D>;
        private var _hangList:Array;
        private var _container:Obj3dContainer;
        private var fightOnAction:String;
        private var fightOnFrame:int;
        private var _callBack:Function;
        private var _useXray:Boolean = false;
        private var _xrayMaterial:Dictionary;
        private var _useColorLight:Boolean = false;
        private var _colorLightFilter:ColorLightFilter;
        private var _colorLightMaterials:Dictionary;
        private var _useSecondMaterial:Boolean = false;
        private var _secondDataObj:Object = null;
        private var _secondMaterial:Dictionary;
        private var _useOriginTexture:Boolean = false;
        private var _useChange:Boolean = false;
        private var _changeState:Boolean = false;
        private var _delay:Number = 10;
        private var _duration:Number = 10;
        private var _noDuration:Boolean = false;
        private var xrayFilter:XRayFilter;
        private static const transform:TransformFilter = new TransformFilter();
        private static var templeMesh:Mesh3D = new Mesh3D("tmp", false, null);

        public function TempleRole(param1:Obj3dContainer)
        {
            this._container = param1;
            return;
        }// end function

        public function get targetRole() : Pivot3D
        {
            return this._targetMesh;
        }// end function

        public function parsersParams(param1) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Number = NaN;
            this._hangList = param1.hangList;
            this.fightOnAction = param1.fightOnAction;
            this.fightOnFrame = param1.fightOnframe;
            if (this._useChange)
            {
                this.changeState = false;
            }
            this._useColorLight = param1.useColorLight ? (param1.useColorLight) : (false);
            if (this._useColorLight)
            {
                this._colorLightMaterials = new Dictionary(false);
                _loc_2 = param1.colorLight ? (param1.colorLight) : (0);
                this._colorLightFilter = new ColorLightFilter();
                this._colorLightFilter.colorOffsetValue[0] = (_loc_2 >> 16 & 255) / 255;
                this._colorLightFilter.colorOffsetValue[1] = (_loc_2 >> 8 & 255) / 255;
                this._colorLightFilter.colorOffsetValue[2] = (_loc_2 & 255) / 255;
            }
            this._useXray = param1.useXray ? (param1.useXray) : (false);
            if (this._useXray)
            {
                this._xrayMaterial = new Dictionary(false);
                _loc_3 = param1.xrayColor ? (param1.xrayColor) : (3362278);
                this.xrayFilter = new XRayFilter();
                this.xrayFilter.rimColor[0] = (_loc_3 >> 16 & 255) / 255;
                this.xrayFilter.rimColor[1] = (_loc_3 >> 8 & 255) / 255;
                this.xrayFilter.rimColor[2] = (_loc_3 & 255) / 255;
            }
            this._delay = param1.delay != null ? (param1.delay) : (10);
            this._duration = param1.duration != null ? (param1.duration) : (10);
            this._useSecondMaterial = param1.useSecondMaterial ? (param1.useSecondMaterial) : (false);
            this._secondDataObj = param1.secondData;
            this._useOriginTexture = param1.useOriginTex ? (param1.useOriginTex) : (false);
            if (this._useSecondMaterial)
            {
                this._secondMaterial = new Dictionary(false);
            }
            this._noDuration = param1.noDuration ? (param1.noDuration) : (false);
            this._useChange = this._useXray || this._useColorLight || this._useSecondMaterial;
            this._container.templeNeedUpdate = this._useChange;
            this.checkAndPlay();
            return;
        }// end function

        public function setTempleParams(param1) : void
        {
            this.setRoleParams(param1[0], param1[1], param1[2]);
            return;
        }// end function

        public function setRoleParams(param1:Md5Mesh, param2:Function, param3:Vector.<Mesh3D>) : void
        {
            if (param1 == null)
            {
                return;
            }
            if (this._useChange)
            {
                this.changeState = false;
            }
            this._targetMesh = param1;
            this._playerList = param3;
            this._callBack = param2;
            this._container.rehangToTemple();
            this.checkAndPlay();
            return;
        }// end function

        public function unHangAll(param1:Pivot3D, param2:Boolean = false) : void
        {
            if (this._useChange)
            {
                this.changeState = false;
            }
            if (!this._targetMesh)
            {
                return;
            }
            this._targetMesh.targetMd5Controler.removeFrameScript(this.fightOnAction, this.fightOnFrame, this.startFightHander);
            this.removeAllHangObj(param1);
            if (!param2)
            {
                this._container.removeAllHangToTemple(param1);
            }
            return;
        }// end function

        private function removeAllHangObj(param1:Pivot3D) : void
        {
            var _loc_2:Object = null;
            var _loc_3:String = null;
            var _loc_4:String = null;
            var _loc_5:Pivot3D = null;
            for each (_loc_2 in this._hangList)
            {
                
                _loc_3 = _loc_2.uid;
                _loc_4 = _loc_2.boneName;
                _loc_5 = this._container.getObject3dByID(_loc_2.uid);
                if (_loc_5)
                {
                    if (_loc_5.curHangControler)
                    {
                        _loc_5.curHangControler.removeHang(_loc_5);
                        _loc_5.curHangControler = null;
                    }
                    _loc_5.parent = param1;
                }
            }
            return;
        }// end function

        private function startFightHander() : void
        {
            if (!this._container || !this._container.timerContorler || this._container.timerContorler.curFrame < this.fightOnFrame)
            {
                return;
            }
            if (this._callBack != null)
            {
                this._callBack.call();
                this._callBack = null;
            }
            this._targetMesh.targetMd5Controler.removeFrameScript(this.fightOnAction, this.fightOnFrame, this.startFightHander);
            return;
        }// end function

        public function checkAndPlay() : void
        {
            var _loc_1:Object = null;
            var _loc_2:String = null;
            var _loc_3:String = null;
            var _loc_4:Pivot3D = null;
            var _loc_5:Md5SkinAnimateControler = null;
            if (!this._targetMesh || this._container.isDisposed || this._container.isInPool)
            {
                return;
            }
            if (this._useChange)
            {
                this.changeState = false;
            }
            if (this._container.parent != this._targetMesh)
            {
                this._targetMesh.addChild(this._container);
            }
            this._container.updateMaskable(this._targetMesh.maskEnable);
            if (this._callBack != null)
            {
                this._targetMesh.targetMd5Controler.addFrameScript(this.fightOnAction, this.fightOnFrame, this.startFightHander);
                if (this._container.timerContorler.curFrame > this.fightOnFrame)
                {
                    this.startFightHander();
                }
            }
            for each (_loc_1 in this._hangList)
            {
                
                _loc_2 = _loc_1.uid;
                _loc_3 = _loc_1.boneName;
                _loc_4 = this._container.getObject3dByID(_loc_1.uid);
                if (_loc_4 && _loc_4.parent != this._targetMesh)
                {
                    if (_loc_3)
                    {
                        _loc_5 = this._targetMesh.getAnimateControlerInstance(AnimateControlerType.Md5SkinAnimateControler) as Md5SkinAnimateControler;
                        continue;
                    }
                    if (_loc_4.curHangControler)
                    {
                        _loc_4.curHangControler.removeHang(_loc_4);
                        _loc_4.curHangControler = null;
                    }
                    this._targetMesh.addChild(_loc_4);
                    _loc_4.identityOffsetTransform();
                }
            }
            return;
        }// end function

        public function dispose(param1:Pivot3D) : void
        {
            var _loc_2:UseOriTexShaderbase = null;
            var _loc_3:XRayShader = null;
            this.changeState = false;
            this.unHangAll(param1);
            this._container = null;
            this._callBack = null;
            this._targetMesh = null;
            this._playerList = null;
            this._hangList = null;
            this._colorLightFilter = null;
            if (this._secondMaterial)
            {
                for each (_loc_2 in this._secondMaterial)
                {
                    
                    UseOriTexShaderbase.disposeInstance(_loc_2, true);
                }
                this._secondMaterial = null;
            }
            if (this._xrayMaterial)
            {
                for each (_loc_3 in this._secondMaterial)
                {
                    
                    XRayShader.disposeInstance(_loc_3);
                }
                this._xrayMaterial = null;
            }
            this._colorLightMaterials = null;
            return;
        }// end function

        public function getMeshByUid(param1:String) : Pivot3D
        {
            if (param1 == "parent0" || param1 == "parent2")
            {
                return this._targetMesh;
            }
            return null;
        }// end function

        private function changeMaterialToXray() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:Mesh3D = null;
            _loc_1 = this._targetMesh.material.materialParams.skinNum;
            if (!this._xrayMaterial[_loc_1])
            {
                this._xrayMaterial[_loc_1] = XRayShader.getInstanceFromPool(_loc_1, transform, this.xrayFilter);
            }
            this._targetMesh.changeToAdditionalMaterial(this._xrayMaterial[_loc_1]);
            if (this._playerList)
            {
                _loc_2 = this._playerList.length;
                _loc_3 = 0;
                while (_loc_3 < _loc_2)
                {
                    
                    _loc_4 = this._playerList[_loc_3];
                    if (!_loc_4)
                    {
                    }
                    else if (_loc_4 == this._targetMesh)
                    {
                    }
                    else
                    {
                        _loc_1 = _loc_4.material.materialParams.skinNum;
                        if (!this._xrayMaterial[_loc_1])
                        {
                            this._xrayMaterial[_loc_1] = XRayShader.getInstanceFromPool(_loc_1, transform, this.xrayFilter);
                        }
                        _loc_4.changeToAdditionalMaterial(this._xrayMaterial[_loc_1]);
                    }
                    _loc_3++;
                }
            }
            return;
        }// end function

        private function addColorLight() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:Mesh3D = null;
            if (!this._targetMesh.material.materialParams.hasFilter(FilterType.Fragment_ColorLight))
            {
                this._targetMesh.material.materialParams.addFilte(this._colorLightFilter);
                this._colorLightMaterials["role"] = this._targetMesh.material;
            }
            if (this._playerList)
            {
                _loc_1 = this._playerList.length;
                _loc_2 = 0;
                while (_loc_2 < _loc_1)
                {
                    
                    _loc_3 = this._playerList[_loc_2];
                    if (!_loc_3)
                    {
                    }
                    else if (!_loc_3.material.materialParams.hasFilter(FilterType.Fragment_ColorLight))
                    {
                        _loc_3.material.materialParams.addFilte(this._colorLightFilter);
                        this._colorLightMaterials[_loc_2] = _loc_3.material;
                    }
                    _loc_2++;
                }
            }
            return;
        }// end function

        private function clearColorLight() : void
        {
            var _loc_1:ShaderBase = null;
            if (this._colorLightMaterials)
            {
                for each (_loc_1 in this._colorLightMaterials)
                {
                    
                    if (_loc_1)
                    {
                        if (_loc_1.materialParams.hasFilter(FilterType.Fragment_ColorLight))
                        {
                            _loc_1.materialParams.removeFilte(this._colorLightFilter);
                        }
                        _loc_1 = null;
                    }
                }
            }
            return;
        }// end function

        private function createSecNotOrigin(param1:int) : UseOriTexShaderbase
        {
            var _loc_2:* = UseOriTexShaderbase.getInstanceFromPool("secondMbone" + param1, this._useOriginTexture, transform, null);
            templeMesh.changeOriginalMaterial(_loc_2);
            this._secondDataObj.isDepthWrite = true;
            ParserLayerObject.instance.setObj3dMaterialProperty(templeMesh, this._secondDataObj);
            _loc_2.materialParams.skinNum = param1;
            return _loc_2;
        }// end function

        private function changeMaterialToSecond() : void
        {
            var _loc_1:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:Mesh3D = null;
            _loc_1 = this._targetMesh.material.materialParams.skinNum;
            var _loc_2:* = this._secondMaterial[100];
            var _loc_6:* = this.createSecNotOrigin(_loc_1);
            _loc_2 = this.createSecNotOrigin(_loc_1);
            var _loc_6:* = _loc_6;
            this._secondMaterial[100] = _loc_6;
            var _loc_6:* = _loc_1;
            _loc_2.materialParams.skinNum = _loc_1;
            _loc_2.useOriginTexture = this._useOriginTexture;
            this._targetMesh.changeToAdditionalMaterial(_loc_2);
            if (this._playerList)
            {
                _loc_3 = this._playerList.length;
                _loc_4 = 0;
                while (_loc_4 < _loc_3)
                {
                    
                    _loc_5 = this._playerList[_loc_4];
                    if (!_loc_5)
                    {
                    }
                    else if (_loc_5 == this._targetMesh)
                    {
                    }
                    else
                    {
                        _loc_1 = _loc_5.material.materialParams.skinNum;
                        _loc_2 = this._secondMaterial[_loc_4];
                        var _loc_6:* = this.createSecNotOrigin(_loc_1);
                        _loc_2 = this.createSecNotOrigin(_loc_1);
                        var _loc_6:* = _loc_6;
                        this._secondMaterial[_loc_4] = _loc_6;
                        var _loc_6:* = _loc_1;
                        _loc_2.materialParams.skinNum = _loc_1;
                        _loc_2.useOriginTexture = this._useOriginTexture;
                        _loc_5.changeToAdditionalMaterial(_loc_2);
                    }
                    _loc_4++;
                }
            }
            return;
        }// end function

        public function update() : void
        {
            var _loc_1:int = 0;
            if (this._useChange)
            {
                if (this._noDuration)
                {
                    _loc_1 = this._container.timerContorler.totalframe;
                    if (_loc_1 >= this._delay)
                    {
                        this.changeState = true;
                    }
                    else
                    {
                        this.changeState = false;
                    }
                }
                else
                {
                    _loc_1 = this._container.timerContorler.curFrame;
                    if (_loc_1 >= this._delay && _loc_1 < this._delay + this._duration)
                    {
                        this.changeState = true;
                    }
                    else
                    {
                        this.changeState = false;
                    }
                }
            }
            return;
        }// end function

        private function get changeState() : Boolean
        {
            return this._changeState;
        }// end function

        private function set changeState(param1:Boolean) : void
        {
            if (this._changeState == param1 || !this._targetMesh)
            {
                return;
            }
            this._changeState = param1;
            if (this._changeState)
            {
                if (this._useXray)
                {
                    this.changeMaterialToXray();
                }
                else if (this._useSecondMaterial)
                {
                    this.changeMaterialToSecond();
                }
                else if (this._useColorLight)
                {
                    this.addColorLight();
                }
            }
            else if (this._useColorLight)
            {
                this.clearColorLight();
            }
            else if (this._useXray)
            {
                this.clearXray();
            }
            else if (this._useSecondMaterial)
            {
                this.clearSecondMaterial();
            }
            return;
        }// end function

        private function clearXray() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:Mesh3D = null;
            var _loc_1:* = this._targetMesh.material.materialParams.skinNum;
            this._targetMesh.removeAdditionalMaterial(this._xrayMaterial[_loc_1]);
            if (this._playerList)
            {
                _loc_2 = this._playerList.length;
                _loc_3 = 0;
                while (_loc_3 < _loc_2)
                {
                    
                    _loc_4 = this._playerList[_loc_3];
                    if (!_loc_4)
                    {
                    }
                    else if (_loc_4 == this._targetMesh)
                    {
                    }
                    else
                    {
                        _loc_1 = _loc_4.material.materialParams.skinNum;
                        _loc_4.removeAdditionalMaterial(this._xrayMaterial[_loc_1]);
                    }
                    _loc_3++;
                }
            }
            return;
        }// end function

        private function clearSecondMaterial() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:Mesh3D = null;
            this._targetMesh.removeAdditionalMaterial(this._secondMaterial[100]);
            if (this._playerList)
            {
                _loc_1 = this._playerList.length;
                _loc_2 = 0;
                while (_loc_2 < _loc_1)
                {
                    
                    _loc_3 = this._playerList[_loc_2];
                    if (!_loc_3)
                    {
                    }
                    else if (_loc_3 == this._targetMesh)
                    {
                    }
                    else if (this._secondMaterial[_loc_2])
                    {
                        _loc_3.removeAdditionalMaterial(this._secondMaterial[_loc_2]);
                    }
                    _loc_2++;
                }
            }
            return;
        }// end function

    }
}
